//
//  ProductHomeViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15/7/22.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "ProductHomeViewController.h"
#import "ProductHomeTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface ProductHomeViewController ()
{
    NSMutableArray *sectionTitleNameArray;
    NSMutableArray * sectionContentArray;
    NSMutableArray * allContentArray;
    NSMutableDictionary *productHomeDict;
    NSString *imageNameIndexStr;
    NSMutableArray *productHomeImageIndexArray;
    
     NSString *contentIndexStr;
     NSMutableArray *dictContentIndexArray;
}
@end

@implementation ProductHomeViewController
-(void)viewWillAppear:(BOOL)animated
{
   // if ([Global getLoginToken].length>0) {
     sectionTitleNameArray=[NSMutableArray array];
    sectionContentArray=[NSMutableArray array];
    allContentArray=[NSMutableArray array];
    productHomeImageIndexArray=[NSMutableArray array];
    dictContentIndexArray=[NSMutableArray array];
    productHomeDict=[[NSMutableDictionary alloc] init];
    [self requestProductHomeList];
    //}
}
-(void)requestProductHomeList
{
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
       
        //  NSLog(@"paramsLIST====%@",params);
        [HttpRequest requestData:@"Product/SeriesListP" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
//                   NSLog(@"severDataDict.dataDict====%@",severDataDict.dataDict);
                    [Global hideProgressHUDInternal:self.view];
                    for (UIView *v in _productHomeScrollView.subviews) {
                        [v removeFromSuperview];
                    }
                    NSMutableArray *mSeriesArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"mSeries"]];
                 //   productHomeDict=[severDataDict.dataDict objectForKey:@"mSeries"];
                    if ( mSeriesArray.count>0) {
                        for (int i=0; i< mSeriesArray.count; i++) {
                            [sectionTitleNameArray addObject:[[mSeriesArray objectAtIndex:i] objectForKey:@"ItypeName"]];
                            [sectionContentArray addObject:[[mSeriesArray objectAtIndex:i] objectForKey:@"DataLine"]];
                            
                               }
                        
                        
                    }
                    
                    float scrollViewHeight=0.0f;
                   
                    // NSLog(@"sectionContentArray===%@",sectionContentArray);
                    for (int j=0; j<sectionContentArray.count; j++) {
                       // [allContentArray addObject:[[sectionContentArray objectAtIndex:j] objectForKey:@"ItypeName"]];
                        UILabel *lineLabel=[[UILabel alloc] init];
                        // lineLabel.tag=9;
                        lineLabel.frame=CGRectMake(25,20+scrollViewHeight,3, 20);
                        [lineLabel setBackgroundColor:[UIColor colorWithRed:150.0f/255.0f green:150.0f/255.0f blue:150.0f/255.0f alpha:1.0f]];
                        [_productHomeScrollView addSubview:lineLabel];
                        //lineLabel=nil;
                        
                        UILabel *titleLabel=[[UILabel alloc] init];
                        titleLabel.font = [UIFont fontWithName:@"Helvetica" size:18];
                        // lineLabel.tag=9;
                        titleLabel.text=[sectionTitleNameArray objectAtIndex:j] ;
                         [titleLabel setTextColor:[UIColor colorWithRed:160.0f/255.0f green:160.0f/255.0f blue:160.0f/255.0f alpha:1.0f]];
                        titleLabel.frame=CGRectMake(lineLabel.frame.origin.x+lineLabel.frame.size.width+8, lineLabel.frame.origin.y+4,200, 15);
                        [titleLabel setBackgroundColor:[UIColor clearColor]];
                        [_productHomeScrollView addSubview:titleLabel];
                        //titleLabel=nil;
                        
             
                      // scrollViewHeight=scrollViewHeight+20;
                        NSMutableArray *array=[NSMutableArray arrayWithArray:[sectionContentArray objectAtIndex:j]];
                        for (int m=0; m<array.count; m++) {
                            UIButton *imageButton=[UIButton buttonWithType:UIButtonTypeCustom];
                            CGRect rect=imageButton.frame;
                            imageButton.tag=m;
                            rect.size.height=90;
                            rect.size.width=90;
                            rect.origin.x=45+(105+45)*(m%6);
                            rect.origin.y=lineLabel.frame.origin.y+lineLabel.frame.size.height+30+160*(m/6);
                            // NSLog(@"i/6=====%d",i/5);
                            
                            imageButton.frame=rect;
                            imageButton.titleLabel.text=[[array objectAtIndex:m] objectForKey:@"ItypeCode"];
                            imageButton.titleLabel.textColor=[UIColor clearColor];
                            // viewHeight=rect.origin.y+rect.size.height+15;
                             [imageButton addTarget:self action:@selector(imageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                            UIImageView *sampleImageView=[[UIImageView alloc] initWithFrame:imageButton.frame];
                          //  sampleImageView.tag=i+1+100;
                            if([[[array objectAtIndex:m] objectForKey:@"ImageUrl"] length]>0)
                            {
                            [sampleImageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:m] objectForKey:@"ImageUrl"]]];
                            }
                            else
                            {
                                [sampleImageView setImage:[UIImage imageNamed:@"zhanweitu_.png"]];
                            }
                            
                            UILabel *nameLabel=[[UILabel alloc] init];
                            nameLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                            // lineLabel.tag=9;
                            nameLabel.text=[[array objectAtIndex:m] objectForKey:@"ISectypeName"];
                            nameLabel.textAlignment=NSTextAlignmentCenter;
                            [nameLabel setTextColor:[UIColor colorWithRed:160.0f/255.0f green:160.0f/255.0f blue:160.0f/255.0f alpha:1.0f]];
                            nameLabel.frame=CGRectMake(imageButton.frame.origin.x,imageButton.frame.origin.y+imageButton.frame.size.height+15,imageButton.frame.size.width ,15);
                            [nameLabel setBackgroundColor:[UIColor clearColor]];
                            scrollViewHeight=nameLabel.frame.origin.y+80;
                            
                            [_productHomeScrollView addSubview:nameLabel];
                            [_productHomeScrollView addSubview:sampleImageView];
                            [_productHomeScrollView addSubview:imageButton];
                            
                        }
                    }
                    [_productHomeScrollView setContentSize:CGSizeMake(0, scrollViewHeight)];
                    [_productHomeScrollView setScrollEnabled:YES];

//                    NSLog(@"productHomeDict===%@",productHomeDict);
//                   
                    
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    
//                    NSLog(@"requestResultStr=====%@",requestResultStr);
                    [Global showTextHudSub:requestResultStr view:self.view];
//                    UIViewController *controller=[[GlobalViewController alloc]init];
//                    [self.navigationController pushViewController:controller animated:YES];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    [Global loginViewController:self noteStr:requestResultStr];
                     //   [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
}
-(void)imageButtonClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(productHomeButtonClick:selectTypeID:)]) {
       // NSLog(@"selectCodeStr===%@",selectCodeStr);
        [self.delegate productHomeButtonClick:sender.titleLabel.text selectTypeID:sender.tag];
    }

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
//-(void)dealloc {
//    [super dealloc ];
//    NSLog(@"rrrewrwrwrwrwrwr");
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
